package com.lanxiao.cloud.modules.dict.application;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.core.utils.StringUtils;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.modules.dict.application.assembler.DictTypeAssembler;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypePageDto;
import com.lanxiao.cloud.modules.dict.domain.model.DictType;
import com.lanxiao.cloud.modules.dict.infrastructure.repository.DictMapper;
import com.lanxiao.cloud.modules.dict.infrastructure.repository.DictTypeMapper;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-26
 */
@Component
public class DictTypeQueryManager implements ILog<DictType, Long> {

  private final DictTypeMapper dictTypeMapper;
  private final DictTypeAssembler dictTypeAssembler;

  public DictTypeQueryManager(
      DictTypeMapper dictTypeMapper,
      DictTypeAssembler dictTypeAssembler) {
    this.dictTypeMapper = dictTypeMapper;
    this.dictTypeAssembler = dictTypeAssembler;
  }

  public ResultModel<DictTypePageDto> page(String type, PageQuery pageQuery) {
    return dictTypeAssembler.toDictTypePageDto(dictTypeMapper.selectPage(
        new Page<>(pageQuery.getPage(), pageQuery.getSize()), new LambdaQueryWrapper<DictType>()
                .eq(StringUtils.isNotBlank(type), DictType::getType, type)
            .eq(DictType::getDelFlag, DelFlagEnum.NOT_DELETE)));
  }

  @Override
  public DictType selectById(Long id) {
    return dictTypeMapper.selectById(id);
  }

}
